const LOCAL_KEY = "todos";

/**
 * 使用时间戳和随机字母组成唯一的ID
 * @returns {String} ID
 */
export function generateID() {
	return Date.now() + Math.random().toString(36).substring(2, 6);
}

/**
 * 获取localStorage中的todoList
 * @returns {Array} todoList
 */
export function getTodos() {
	let result = localStorage.getItem(LOCAL_KEY);
	if (result) {
		return JSON.parse(result);
	} else {
		return [];
	}
}

/**
 * 将todoList保存到localStorage中
 * @param {Array} todos
 */
export function saveTodos(todos) {
	localStorage.setItem(LOCAL_KEY, JSON.stringify(todos));
}

/**
 * 根据ID删除指定的todo
 * @param {String} todoID todo的ID
 */
export function deleteTodo(todoID) {
	let todoList = getTodos();
	todoList = todoList.filter((item) => item.id != todoID);
	saveTodos(todoList);
}
